/*
 * File:   BlackBoxTestsTableTranscriber.cpp
 * Author: Guilherme Monteiro
 *
 */

#include "BlackBoxTestsTableTranscriber.h"


CPPUNIT_TEST_SUITE_REGISTRATION(BlackBoxTestsTableTranscriber);

const int ARRAY_SIZE = 100;
string paths[ARRAY_SIZE] = {
        "/books/estatisticas/baixa_resolucao/image49.png",
        "/books/estatisticas/baixa_resolucao/image62.png",
        "/books/estatisticas/baixa_resolucao/image121.png",
        "/books/estatisticas/baixa_resolucao/image152.png",
        "/books/estatisticas/baixa_resolucao/image35.png",
        "/books/estatisticas/baixa_resolucao/image37.png",
        "/books/estatisticas/baixa_resolucao/image40.png",
        "/books/estatisticas/baixa_resolucao/image45.png",
        "/books/estatisticas/baixa_resolucao/image76.png",
        "/books/estatisticas/baixa_resolucao/image77.png",
        "/books/estatisticas/baixa_resolucao/image79.png",
        "/books/estatisticas/baixa_resolucao/image84.png",
        "/books/estatisticas/baixa_resolucao/image98.png",
        "/books/estatisticas/baixa_resolucao/image100.png",
        "/books/estatisticas/baixa_resolucao/image106.png",
        "/books/estatisticas/baixa_resolucao/image107.png",
        "/books/estatisticas/baixa_resolucao/image123.png",
        "/books/estatisticas/baixa_resolucao/image126.png",
        "/books/estatisticas/baixa_resolucao/image158.png",
        
        "/books/mensagem/baixa_resolucao/image56.png",
        "/books/mensagem/baixa_resolucao/image59.png",
        "/books/mensagem/baixa_resolucao/image83.png",
        "/books/mensagem/baixa_resolucao/image84.png",
        "/books/mensagem/baixa_resolucao/image99.png",  // rotacionada 90 graus = [23]
        "/books/mensagem/baixa_resolucao/image101.png",
        "/books/mensagem/baixa_resolucao/image107.png", // rotacionada 90 graus = [25]
        "/books/mensagem/baixa_resolucao/image109.png",
        "/books/mensagem/baixa_resolucao/image110.png", 
        "/books/mensagem/baixa_resolucao/image112.png",
        "/books/mensagem/baixa_resolucao/image123.png",
        
        "/books/caracterizacao/baixa_resolucao/image81_t.png", // rotacionada 90 graus = [30]
        "/books/caracterizacao/baixa_resolucao/image133_t.png", // rotacionada 90 graus = [31]
        "/books/caracterizacao/baixa_resolucao/image278.png",
                
        "/books/estatisticas/baixa_resolucao/image1.png", // images to software quality measurement
        "/books/caracterizacao/baixa_resolucao/image59.png",
        "/books/caracterizacao/baixa_resolucao/image64.png",
        "/books/caracterizacao/baixa_resolucao/image139.png", // rotacionada 90 graus = [3]
        "/books/estatisticas/baixa_resolucao/image15.png",
        "/books/caracterizacao/baixa_resolucao/image82.png",
        "/books/estatisticas/baixa_resolucao/image4.png",
        "/books/estatisticas/baixa_resolucao/image24.png",
        "/books/caracterizacao/baixa_resolucao/image63.png",
        "/books/caracterizacao/baixa_resolucao/image66.png"
};

BlackBoxTestsTableTranscriber::BlackBoxTestsTableTranscriber() {
}

BlackBoxTestsTableTranscriber::~BlackBoxTestsTableTranscriber() {
}

void BlackBoxTestsTableTranscriber::setUp() {
}

void BlackBoxTestsTableTranscriber::tearDown() {
}

void BlackBoxTestsTableTranscriber::caseTestUsingModelEmpty() {
    try {
        ModelListHandler* modelListHandler = new ModelListHandler("");
        vector<GenericModel*> modelsList = modelListHandler->getModelList();

        TableTranscriber* tableTranscriber = new TableTranscriber(paths[0], modelsList[0], "");
        tableTranscriber->run();   
    }
    catch(NullImageException &nie) {
        cerr << nie.what() << endl;
        CPPUNIT_ASSERT(false);
    }
    catch(InvalidModelException &ime) {
        string msg = "ERROR ==> An invalid model gone passed as argument, verify it and try again.";
        if(strcmp(ime.what(), msg.c_str()) != 0) {
            CPPUNIT_ASSERT(false);
        }
    }
}

void BlackBoxTestsTableTranscriber::caseTestUsingInvalidModelsList() {
    try {
        ModelListHandler* modelListHandler = new ModelListHandler("model2,model1");
        vector<GenericModel*> modelsList = modelListHandler->getModelList();
        
        vector<GenericModel*>::iterator itModels;
        for(itModels = modelsList.begin(); itModels != modelsList.end(); itModels++) {
            TableTranscriber* tableTranscriber = new TableTranscriber(paths[23], *itModels, "-r");
            tableTranscriber->run();   
        }
    }
    catch(InvalidModelException &ime) {
        string msg = "ERROR ==> An invalid model gone passed as argument, verify it and try again.";
        if(strcmp(msg.c_str(), ime.what()) != 0) {
            CPPUNIT_ASSERT(false);
        }
    }
    catch(exception &e) {
        cerr << e.what() << endl;
        CPPUNIT_ASSERT(false);
    }
}

void BlackBoxTestsTableTranscriber::caseTestUsingBookEmpty() {
    string p = "";
    try {
        ModelListHandler* modelListHandler = new ModelListHandler("model1");
        vector<GenericModel*> modelsList = modelListHandler->getModelList();

        TableTranscriber* tableTranscriber = new TableTranscriber(p, modelsList[0], "");
        tableTranscriber->run();   
    }
    catch(NullImageException &nie) {
        string msg = "ERROR ==> Image " + p + " can't be loaded. Change the image path or the directory of execution";
        if(strcmp(nie.what(), msg.c_str()) != 0) {
            CPPUNIT_ASSERT(false);
        }
    }
    catch(exception &e) {
        cerr << e.what() << endl;
        CPPUNIT_ASSERT(false);
    }
}

void BlackBoxTestsTableTranscriber::caseTestUsingInvalidBook() {
    string p2 = "books.png";
    try {
        ModelListHandler* modelListHandler = new ModelListHandler("model1");
        vector<GenericModel*> modelsList = modelListHandler->getModelList();

        TableTranscriber* tableTranscriber = new TableTranscriber(p2, modelsList[0], "-nr");
        tableTranscriber->run();   
    }
    catch(NullImageException &nie) {
        string msg = "ERROR ==> Image " + p2 + " can't be loaded. Change the image path or the directory of execution";
        //printf("nie.what(): %s\n", nie.what());
        if(strcmp(nie.what(), msg.c_str()) != 0) {
            CPPUNIT_ASSERT(false);
        }
    }
    catch(exception e) {
        cerr << e.what() << endl;
        CPPUNIT_ASSERT(false);
    }
}

void BlackBoxTestsTableTranscriber::caseTestUsingModel1NotRotated90Degrees() {
    try {
        ModelListHandler* modelListHandler = new ModelListHandler("model1");
        vector<GenericModel*> modelsList = modelListHandler->getModelList();

        TableTranscriber* tableTranscriber = new TableTranscriber(paths[0], modelsList[0], "-nr");
        tableTranscriber->run();   
    }
    catch(NullImageException &nie) {
        cerr << nie.what() << endl;
        CPPUNIT_ASSERT(false);
    }
    catch(exception &e) {
        cerr << e.what() << endl;
        CPPUNIT_ASSERT(false);
    }
}

void BlackBoxTestsTableTranscriber::caseTestUsingModel2NotRotated90Degrees() {
    try {
        ModelListHandler* modelListHandler = new ModelListHandler("model2");
        vector<GenericModel*> modelsList = modelListHandler->getModelList();

        TableTranscriber* tableTranscriber = new TableTranscriber(paths[0], modelsList[0], "-nr");
        tableTranscriber->run();   
    }
    catch(NullImageException &nie) {
        cerr << nie.what() << endl;
        CPPUNIT_ASSERT(false);
    }
    catch(exception &e) {
        cerr << e.what() << endl;
        CPPUNIT_ASSERT(false);
    }
}

void BlackBoxTestsTableTranscriber::caseTestUsingMultiplesNotRotated90DegreesTablesIntoOnePage() {
    try {
        ModelListHandler* modelListHandler = new ModelListHandler("model1");
        vector<GenericModel*> modelsList = modelListHandler->getModelList();

        TableTranscriber* tableTranscriber = new TableTranscriber(paths[0], modelsList[0], "-nr");
        tableTranscriber->run();   
    }
    catch(InvalidRectException &ire) {
        cerr << ire.what() << endl;
        CPPUNIT_ASSERT(false);
    }
    catch(exception &e) {
        cerr << e.what() << endl;
        CPPUNIT_ASSERT(false);
    }
}